<template>
    <div style="position: fixed;width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;">
        <Loading size="24px" vertical>加载中...</Loading>
    </div>
</template>

<script>
    import {getUserInfo} from '@api/ihub'
    import {getToken} from '@api/schedule'
    import {Loading} from 'vant';

    export default {
        name: "Auth",
        data() {
            return {
                result: {}
            }
        },
        components: {
            Loading
        },
        methods: {},
        mounted() {
            //将ihub的回调ticket保存
            let ticket = this.$route.query.ticket
            setTimeout(sessionStorage.setItem('ihub-ticket', ticket), 500)
            if (ticket) {
                //获取当前微信用户信息
                getUserInfo({}).then(res => {
                    if (res.status == "1000") {
                        //保存微信用户信息
                        this.result = res.data.Array[0]
                        sessionStorage.setItem('bu', res.data.Array[0].bu)
                        sessionStorage.setItem('name', res.data.Array[0].name)
                        sessionStorage.setItem('email', res.data.Array[0].email)
                        sessionStorage.setItem('avatar', res.data.Array[0].thumb_avatar)
                        let email = res.data.Array[0].email
                        if (email) {
                            getToken({
                                // account: 'admin@sanofi.com'
                                account: email
                            }).then(res => {
                                if (res.successful) {
                                    if (res.data.token != '' && res.data.token != null) {
                                        sessionStorage.setItem('access-token', res.data.token)
                                        this.$router.replace('/landing')
                                    } else {
                                        alert(res.data.token)
                                    }
                                } else {
                                    alert(res.message)
                                }
                            })
                        } else {
                            alert('未获取到email')
                        }
                    } else {
                        alert(res.data.rtnMsg)
                    }

                })
            } else {
                alert('未获取到ticket')
            }
        }
    }
</script>

<style scoped>

</style>